package com.Jian.reggie.controller;

import com.Jian.reggie.common.R;
import com.Jian.reggie.dto.DishDto;
import com.Jian.reggie.impl.Dish;
import com.Jian.reggie.impl.DishFlavor;
import com.Jian.reggie.service.CategoryService;
import com.Jian.reggie.service.DishFlavorService;
import com.Jian.reggie.service.DishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ClassName: DishController
 * Package: com.Jian.reggie.controller
 * Description:
 *
 * @Author 蹇泽阳
 * @Create 2023/11/27 15:08
 * @Version 1.0
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {
  @Autowired
  private DishService dishService;
  @Autowired
  private DishFlavorService dishFlavor;
  @Autowired
  private CategoryService categoryService;
  
  @PostMapping
  public R<String> addDish(@RequestBody DishDto dishDto) {
    // 新增菜品，同时保存对应口味数据
    dishService.saveWithFlavor(dishDto);
    return R.success("新增菜品成功");
  }
  
  /**
   * 分页查询
   */
  @GetMapping("/page")
  public R<Page<DishDto>> page(int page, int pageSize, String name) {
    Page<Dish> dishpage = new Page<>(page, pageSize);
    Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
    
    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.like(name != null, Dish::getName, name);
    
    queryWrapper.orderByAsc(Dish::getUpdateTime);
    Page<Dish> dishPage = dishService.page(dishpage, queryWrapper);
    
    // 对象拷贝,将dishPage中的数据拷贝到dishDtoPage中,忽略records属性
    BeanUtils.copyProperties(dishPage, dishDtoPage, "records");
    List<DishDto> list = dishpage.getRecords().stream().map(dish -> {
      DishDto dishDto = new DishDto();
      BeanUtils.copyProperties(dish, dishDto);
      Long categoryId = dish.getCategoryId();
      String categoryName = categoryService.getById(categoryId).getName();
      dishDto.setCategoryName(categoryName);
      return dishDto;
    }).collect(Collectors.toList());
    
    dishDtoPage.setRecords(list);
    
    return R.success(dishDtoPage);
  }
  
  /**
   * 根据id修改菜品
   */
  @GetMapping("/{id}")
  public R<DishDto> getDish(@PathVariable Long id) {
    DishDto dishDto = new DishDto();
    Dish dish = dishService.getById(id);
    BeanUtils.copyProperties(dish, dishDto);
    List<DishFlavor> flavors = dishFlavor.listByDishId(id);
    dishDto.setFlavors(flavors);
    return R.success(dishDto);
  }
  
  /**
   * 保存修改的菜品
   */
  @PutMapping
  public R<String> updateDish(@RequestBody DishDto dishDto) {
    dishService.updateWithFlavor(dishDto);
    
    return R.success("修改菜品成功");
  }
  
  /**
   * 查找对应分类ID下的菜品
   */
  // @GetMapping("list")
  // public R<List<Dish>> list(Long categoryId) {
  //   LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
  //   queryWrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
  //   queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
  //   List<Dish> list = dishService.list(queryWrapper);
  //   return R.success(list);
  // }
  @GetMapping("list")
  public R<List<DishDto>> list(Long categoryId) {
    LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    queryWrapper.eq(categoryId != null, Dish::getCategoryId, categoryId);
    queryWrapper.orderByAsc(Dish::getSort).orderByAsc(Dish::getUpdateTime);
    List<Dish> list = dishService.list(queryWrapper);
    
    List<DishDto> dishDtoList = list.stream().map(dish -> {
      DishDto dishDto = new DishDto();
      BeanUtils.copyProperties(dish, dishDto);
      List<DishFlavor> flavors = dishFlavor.listByDishId(dish.getId());
      dishDto.setFlavors(flavors);
      return dishDto;
    }).collect(Collectors.toList());
    return R.success(dishDtoList);
  }
}
